<?php
/**
 * Created by PhpStorm.
 * User: yangwang
 * Date: 2024-04-07
 * Time: 14:38
 */

namespace App\Models\Cruise;


use App\Models\BaseModel;
use Illuminate\Support\Carbon;

class ShopSelfCheckNotifyConfigModel extends BaseModel
{
    protected $table = 'cruise_shop_self_check_notify_config';

    //开启关闭，大开关设置为关闭
    const SWITCH_CLOSE = 0;
    const SWITCH_OPEN  = 1;

    const SWITCH_DESC_MAP = [
        self::SWITCH_CLOSE => '关闭',
        self::SWITCH_OPEN  => '开启'
    ];

    //提醒角色
    const NOTIFY_ROLE_1 = 1;
    const NOTIFY_ROLE_2 = 2;
    const NOTIFY_ROLE_3 = 3;
    const NOTIFY_ROLE_4 = 4;
    const NOTIFY_ROLE_5 = 5;
    const NOTIFY_ROLE_6 = 6;

    const NOTIFY_ROLE_DESC_MAP = [
        self::NOTIFY_ROLE_1 => '店长',
        self::NOTIFY_ROLE_2 => '订货',
        self::NOTIFY_ROLE_3 => '店员',
        self::NOTIFY_ROLE_4 => '收银员',
//        self::NOTIFY_ROLE_5 => '临时员工',//app不让临时工登录
        self::NOTIFY_ROLE_6 => '演示',
    ];

    //提醒方式
    const NOTIFY_TYPE_APP = 1;
    const NOTIFY_TYPE_SMG = 2;

    const NOTIFY_TYPE_DESC_MAP = [
        self::NOTIFY_TYPE_APP => 'app',
        self::NOTIFY_TYPE_SMG => '短信'
    ];

    const SHOP_BUSINESS_OPEN_DEFAULT  = 600;//分钟
    const SHOP_BUSINESS_CLOSE_DEFAULT = 1320;//分钟

    const SHOP_BUSINESS_DESC_MAP = [
        self::SHOP_BUSINESS_OPEN_DEFAULT  => '默认开店时间',
        self::SHOP_BUSINESS_CLOSE_DEFAULT => '默认关店时间'
    ];

    //提醒时间map
    const NOTIFY_TIME_MAP_0 = 0;//不提醒

    const NOTIFY_TIME_MAP_1 = 1;//营业开始时
    const NOTIFY_TIME_MAP_2 = 2;//营业结束时

    const NOTIFY_TIME_MAP_3 = 3;//周一营业时
    const NOTIFY_TIME_MAP_4 = 4;//月初营业开始时

    const NOTIFY_TIME_MAP_5 = 5;//提前5分钟
    const NOTIFY_TIME_MAP_6 = 6;//提前10分钟
    const NOTIFY_TIME_MAP_7 = 7;//提前15分钟
    const NOTIFY_TIME_MAP_8 = 8;//提前30分钟
    const NOTIFY_TIME_MAP_9 = 9;//提前60分钟

    const NOTIFY_TIME_MAP_10 = 10;//每天营业开始时
    const NOTIFY_TIME_MAP_11 = 11;//周一至周五营业开始时
    const NOTIFY_TIME_MAP_12 = 12;//周六至周日开始时
    const NOTIFY_TIME_MAP_13 = 13;//月末3天营业开始时
    const NOTIFY_TIME_MAP_14 = 14;//月末5天营业开始时

    const NOTIFY_TIME_DESC_MAP = [
        self::NOTIFY_TIME_MAP_0  => '不提醒',
        self::NOTIFY_TIME_MAP_1  => '营业开始时',
        self::NOTIFY_TIME_MAP_2  => '营业结束时',
        self::NOTIFY_TIME_MAP_3  => '周一营业时',
        self::NOTIFY_TIME_MAP_4  => '月初营业开始时',
        self::NOTIFY_TIME_MAP_5  => '提前5分钟',
        self::NOTIFY_TIME_MAP_6  => '提前10分钟',
        self::NOTIFY_TIME_MAP_7  => '提前15分钟',
        self::NOTIFY_TIME_MAP_8  => '提前30分钟',
        self::NOTIFY_TIME_MAP_9  => '提前60分钟',
        self::NOTIFY_TIME_MAP_10 => '每天营业开始时',
        self::NOTIFY_TIME_MAP_11 => '周一至周五营业开始时',
        self::NOTIFY_TIME_MAP_12 => '周六至周日营业开始时',
        self::NOTIFY_TIME_MAP_13 => '月末3天营业开始时',
        self::NOTIFY_TIME_MAP_14 => '月末5天营业开始时',
    ];

    const NOTIFY_TIME_KAIDANG_DESC_MAP  = [
        self::NOTIFY_TIME_MAP_0 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_0],
        self::NOTIFY_TIME_MAP_1 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_1],
        self::NOTIFY_TIME_MAP_5 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_5],
        self::NOTIFY_TIME_MAP_6 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_6],
        self::NOTIFY_TIME_MAP_7 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_7],
        self::NOTIFY_TIME_MAP_8 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_8],
        self::NOTIFY_TIME_MAP_9 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_9],
    ];
    const NOTIFY_TIME_SHOUDANG_DESC_MAP = [
        self::NOTIFY_TIME_MAP_0 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_0],
        self::NOTIFY_TIME_MAP_2 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_2],
        self::NOTIFY_TIME_MAP_5 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_5],
        self::NOTIFY_TIME_MAP_6 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_6],
        self::NOTIFY_TIME_MAP_7 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_7],
        self::NOTIFY_TIME_MAP_8 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_8],
        self::NOTIFY_TIME_MAP_9 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_9],
    ];

    const NOTIFY_TIME_WEEKEND_DESC_MAP = [
        self::NOTIFY_TIME_MAP_0  => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_0],
        self::NOTIFY_TIME_MAP_12 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_12],
    ];
    const NOTIFY_TIME_MONTH_DESC_MAP   = [
        self::NOTIFY_TIME_MAP_0  => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_0],
        self::NOTIFY_TIME_MAP_13  => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_13],
        self::NOTIFY_TIME_MAP_14 => self::NOTIFY_TIME_DESC_MAP[self::NOTIFY_TIME_MAP_14],
    ];


    public static function buildMapOptions(array $map, $selected = []): array
    {
        $options['selected'] = $selected;
        $options['list']     = [];
        foreach ($map as $value => $label) {
            $options['list'][] = [
                'label' => $label,
                'value' => $value
            ];
        }
        return $options;
    }

    public static function getNotifyTimeByBusinessTime($openTime, $closeTime, $notifyTimeMap)
    {

        switch ($notifyTimeMap) {
            case self::NOTIFY_TIME_MAP_0:
                return 0;

            case self::NOTIFY_TIME_MAP_1:
            case self::NOTIFY_TIME_MAP_3:
            case self::NOTIFY_TIME_MAP_4:
            case self::NOTIFY_TIME_MAP_10:
            case self::NOTIFY_TIME_MAP_11:
            case self::NOTIFY_TIME_MAP_12:
                return $openTime;
            case self::NOTIFY_TIME_MAP_2:
                return $closeTime;

            case self::NOTIFY_TIME_MAP_5:
                return $openTime - 5;
            case self::NOTIFY_TIME_MAP_6:
                return $openTime - 10;
            case self::NOTIFY_TIME_MAP_7:
                return $openTime - 15;
            case self::NOTIFY_TIME_MAP_8:
                return $openTime - 30;
            case self::NOTIFY_TIME_MAP_9:
                return $openTime - 60;

            default:
                return 0;
        }
    }

    //
    public static function canPushByNotifyTimeMap(int $notifyTimeMap, Carbon $carbon = null): bool
    {
        if (!$carbon) {
            $carbon = Carbon::now();
        }

        switch ($notifyTimeMap) {
            case self::NOTIFY_TIME_MAP_0:
                return false;
            case self::NOTIFY_TIME_MAP_1:
            case self::NOTIFY_TIME_MAP_2:
            case self::NOTIFY_TIME_MAP_5:
            case self::NOTIFY_TIME_MAP_6:
            case self::NOTIFY_TIME_MAP_7:
            case self::NOTIFY_TIME_MAP_8:
            case self::NOTIFY_TIME_MAP_9:
            case self::NOTIFY_TIME_MAP_10:
                return true;

            case self::NOTIFY_TIME_MAP_3:
                return 1 == $carbon->dayOfWeek;
            case self::NOTIFY_TIME_MAP_4:
                return 1 == $carbon->format('d');
            case self::NOTIFY_TIME_MAP_11:
                return in_array($carbon->dayOfWeek, range(1, 5));
            case self::NOTIFY_TIME_MAP_12:
                return in_array($carbon->dayOfWeek, [6, 0]);
            case self::NOTIFY_TIME_MAP_13:
                $lastDay = (int)$carbon->daysInMonth;
                return in_array($carbon->format('d'),range($lastDay-2,$lastDay));
            case self::NOTIFY_TIME_MAP_14:
                $lastDay = (int)$carbon->daysInMonth;
                return in_array($carbon->format('d'),range($lastDay-4,$lastDay));

            default:
                return false;
        }
    }

    public static function canPushByBusinessTime(
        int $sysTemplate,int $notifyTimeMap, int $now, string $startAt, string $endAt
    ): bool
    {
        $startAtFormat = str_replace(':', '', $startAt);
        $endAtFormat   = str_replace(':', '', $endAt);

        $startAtSec = strtotime(date("Ymd {$startAtFormat}",$now));
        $endAtSec   = strtotime(date("Ymd {$endAtFormat}",$now));

        if ($startAtSec >= $endAtSec) {
            $endAtSec = strtotime(date("Ymd {$endAtFormat}",$now)) + (24 * 60 * 60);
        }

        $fixTime = 300;//5分钟容错

        if (in_array($sysTemplate,
            [
                CruiseTemplateModel::SYS_TEMPLATE_SELF_OPEN,CruiseTemplateModel::SYS_TEMPLATE_SELF_WEEK,
                CruiseTemplateModel::SYS_TEMPLATE_SELF_MONTH
            ]
        )){
            switch ($notifyTimeMap) {
                case self::NOTIFY_TIME_MAP_0:
                    return false;
                case self::NOTIFY_TIME_MAP_1:
                case self::NOTIFY_TIME_MAP_3:
                case self::NOTIFY_TIME_MAP_4:
                case self::NOTIFY_TIME_MAP_10:
                case self::NOTIFY_TIME_MAP_11:
                case self::NOTIFY_TIME_MAP_12:
                case self::NOTIFY_TIME_MAP_13:
                case self::NOTIFY_TIME_MAP_14:
                    return ($startAtSec <= $now) && ($now <= ($startAtSec + $fixTime));
                case self::NOTIFY_TIME_MAP_5:
                    return (($startAtSec - 5 * 60) <= $now) && ($now <= ($startAtSec - (5 * 60) + $fixTime));

                case self::NOTIFY_TIME_MAP_6:
                    return (($startAtSec - 10 * 60) <= $now) && ($now <= ($startAtSec - 10 * 60 + $fixTime));

                case self::NOTIFY_TIME_MAP_7:
                    return (($startAtSec - 15 * 60) <= $now) && ($now <= ($startAtSec - 15 * 60 + $fixTime));

                case self::NOTIFY_TIME_MAP_8:
                    return (($startAtSec - 30 * 60) <= $now) && ($now <= ($startAtSec - 30 * 60 + $fixTime));
                case self::NOTIFY_TIME_MAP_9:
                    return (($startAtSec - 60 * 60) <= $now) && ($now <= ($startAtSec - 60 * 60 + $fixTime));

                default:
                    return false;
            }
        }

        if ($sysTemplate == CruiseTemplateModel::SYS_TEMPLATE_SELF_CLOSE){
            switch ($notifyTimeMap) {
                case self::NOTIFY_TIME_MAP_0:
                    return false;
                case self::NOTIFY_TIME_MAP_2:
                    return ($endAtSec <= $now) && ($now <= ($endAtSec + $fixTime));

                case self::NOTIFY_TIME_MAP_5:
                    return (($endAtSec - (5 * 60)) <= $now) && ($now <= ($endAtSec - 5 * 60 + $fixTime));

                case self::NOTIFY_TIME_MAP_6:
                    return (($endAtSec - (10 * 60)) <= $now) && ($now <= ($endAtSec - 10 * 60 + $fixTime));

                case self::NOTIFY_TIME_MAP_7:
                    return (($endAtSec - (15 * 60)) <= $now) && ($now <= ($endAtSec - 15 * 60 + $fixTime));

                case self::NOTIFY_TIME_MAP_8:
                    return (($endAtSec - (30 * 60)) <= $now) && ($now <= ($endAtSec - 30 * 60 + $fixTime));

                case self::NOTIFY_TIME_MAP_9:
                    return (($endAtSec - (60 * 60)) <= $now) && ($now <= ($endAtSec - 60 * 60 + $fixTime));

                default:
                    return false;
            }
        }
        return false;
    }


    public static function canPushByTaskExecTime(int $now, string $executeStartHis, string $executeEndHis)
    {
        $executeStartHisFormat = str_replace(':', '', $executeStartHis);
        $executeEndHisFormat   = str_replace(':', '', $executeEndHis);

        //如果跨天超过24小时就会有问题..
        $executeStartSec = strtotime(date("Ymd {$executeStartHisFormat}",$now));
        $executeEndSec   = strtotime(date("Ymd {$executeEndHisFormat}",$now));

        if ($executeStartHisFormat >= $executeEndHisFormat) {
            $executeEndSec = strtotime(date("Ymd {$executeEndHisFormat}",$now)) + (24 * 60 * 60);
        }

        if ($now < $executeStartSec || $now > $executeEndSec) {
            return false;
        }
        return true;
    }

    public static function getDefaultConfig()
    {
        return [
            'notifySwitch'       => self::SWITCH_OPEN,
            'notifyRoles'        => [
                self::NOTIFY_ROLE_1,
                self::NOTIFY_ROLE_2,
                self::NOTIFY_ROLE_3,
                self::NOTIFY_ROLE_4
            ],
            'notifyType'         => [self::NOTIFY_TYPE_APP],
            'kdNotifyTimeMap'    => self::NOTIFY_TIME_MAP_1,
            'sdNotifyTimeMap'    => self::NOTIFY_TIME_MAP_2,
            'weekNotifyTimeMap'  => self::NOTIFY_TIME_MAP_12,
            'monthNotifyTimeMap' => self::NOTIFY_TIME_MAP_14,
        ];
    }

}
